//
//  JTViewCreater.m
//  XJLiver
//
//  Created by Jingjing liu on 2020/6/6.
//  Copyright © 2020 杨俊涛. All rights reserved.
//

#import "JTViewCreater.h"

//屏幕宽高
#define kScreenWidth [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height

@implementation JTViewCreater

+ (UIView *)createViewWithBackgroundColor:(UIColor *)backgroundColor{
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = backgroundColor;
    return view;
}

+ (UILabel *)createLabelWithTextColor:(UIColor *)textColor textAlignment:(NSTextAlignment)textAlignment font:(UIFont *)font{
    UILabel *label = [[UILabel alloc]init];
    label.textColor = textColor;
    label.textAlignment = textAlignment;
    label.font = font;
    return label;
}

+ (UIButton *)createButtonWithTitle:(NSString *)title font:(UIFont *)font textColor:(UIColor *)textColor image:(UIImage *)image{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = font;
    [button setTitleColor:textColor forState:UIControlStateNormal];
    if (image) {
        [button setImage:image forState:UIControlStateNormal];
    }
    return button;
}

+ (UIImageView *)createClipImageView{
    UIImageView *imageView = [[UIImageView alloc]init];
    [imageView setContentScaleFactor:[[UIScreen mainScreen] scale]];
    //裁剪(可能展示不全)
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.clipsToBounds = YES;
    return imageView;
}

+ (UIImageView *)createScaleImageView{
    UIImageView *imageView = [[UIImageView alloc]init];
    return imageView;
}

+ (UIScrollView *)createScrollViewWithDelegate:(id<UIScrollViewDelegate>)delegate contentSize:(CGSize)contentSize pagingEnabled:(BOOL)pagingEnabled{
    UIScrollView *scrollview = [[UIScrollView alloc]init];
    scrollview.showsVerticalScrollIndicator = NO;
    scrollview.showsHorizontalScrollIndicator = NO;
    scrollview.contentSize = contentSize;
    scrollview.pagingEnabled = pagingEnabled;
    scrollview.delegate = delegate;
    return scrollview;
}

+ (UITableView *)createTableViewWithDelegate:(id<UITableViewDelegate,UITableViewDataSource>)delegate style:(UITableViewStyle )style{
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectZero style:style];
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.delegate = delegate;
    tableView.dataSource = delegate;
    tableView.separatorStyle = 0;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, CGFLOAT_MIN)];
    tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, CGFLOAT_MIN)];
    if(@available(iOS 11.0, *)) {
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        tableView.estimatedRowHeight = 0;
        tableView.estimatedSectionFooterHeight = 0;
        tableView.estimatedSectionHeaderHeight = 0;
    } else if([delegate isKindOfClass:[UIViewController class]]){
        UIViewController *vc = (UIViewController *)delegate;
        vc.automaticallyAdjustsScrollViewInsets = false;
    }
    
    return tableView;
}


+ (UICollectionView *)createCollectionViewWithFlowLayout:(UICollectionViewFlowLayout *)layout delegate:(id<UICollectionViewDelegate,UICollectionViewDataSource>)delegate cell:(Class)cellClass{
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
    collectionView.delegate = delegate;
    collectionView.dataSource = delegate;
    
    if(@available(iOS 11.0, *)){
        collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else if ([delegate isKindOfClass:[UIViewController class]]){
        UIViewController *vc = (UIViewController *)delegate;
        vc.automaticallyAdjustsScrollViewInsets = false;
    }
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    [collectionView registerClass:cellClass forCellWithReuseIdentifier:NSStringFromClass(cellClass)];
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    [collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
    collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    return collectionView;
}

+ (CAGradientLayer *)createGradientLayer:(CGRect )layerFrame startColor:(UIColor *)startColor endColor:(UIColor *)endColor cornerRadius:(CGFloat )radius{
    CAGradientLayer * gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = layerFrame;
    gradientLayer.colors = @[(__bridge id)startColor.CGColor,(__bridge id)endColor.CGColor];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 0);
    gradientLayer.locations = @[@0,@1];
    gradientLayer.cornerRadius = radius;
    return gradientLayer;
}

+ (void)createView:(UIView *)shadowView shadowColor:(UIColor *)shadowColor shadowOffset:(CGSize)offsetSize shadowRadius:(CGFloat)radius alpha:(CGFloat )alpha{
    shadowView.layer.shadowColor = shadowColor.CGColor;
    shadowView.layer.shadowOffset = offsetSize;
    shadowView.layer.shadowRadius = radius;
    shadowView.layer.shadowOpacity = 1;
    shadowView.layer.cornerRadius = radius;
}


+ (void)createViewLayer:(UIView *)layer cornerRadius:(CGFloat)radius{
    layer.layer.cornerRadius = radius;
    layer.clipsToBounds = YES;
}
@end
